/*ParallelPlatform Project - A web platform to distribute and share knowledge
Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.*/


GENTICS.Aloha.Save = new GENTICS.Aloha.Plugin("com.parallelplatform.aloha.plugins.Save");
GENTICS.Aloha.Save.languages = ["en", "es"];
GENTICS.Aloha.Save.init = function () {
	var that = this;
	var saveButton = new GENTICS.Aloha.ui.Button({
				iconClass : "GENTICS_button saveBtn",
				size : "small",
				label : this.i18n("button.labelSave"),
				onclick : function () {
					that.save()
				},
				tooltip : that.i18n("button.tooltip")
			});
	GENTICS.Aloha.FloatingMenu.addButton("GENTICS.Aloha.continuoustext",saveButton,that.i18n("button.tabSave"),0);
};

//GENTICS.Aloha.Save.editableSave = new Array();
GENTICS.Aloha.Save.newType = null;
GENTICS.Aloha.Save.urlSave = "./ajax/edit/";

/*
GENTICS.Aloha.Save.addSave = function(editable){
	$.each(GENTICS.Aloha.editables, function(){
	    if( this.obj[0] == editable[0]){
	        GENTICS.Aloha.Save.editableSave.push(this);
	    } 
	})
}

GENTICS.Aloha.Save.resetSave = function(editable){
	GENTICS.Aloha.Save.editableSave = new Array();
}*/

/**
 * Save wrapper, all functions that must happen before saving should be added here
 */
GENTICS.Aloha.Save.save = function () {
	
	// Add here functions to trigger before saving
	//--------------------------------------------
	if($("#subpage").length > 0){
		GENTICS.Aloha.Save.reorderChildrenList();
	}
	
	// -------------------------------------------
	// Saving function:
	GENTICS.Aloha.Save.commitSave();
};

GENTICS.Aloha.Save.reorderChildrenList = function(){
	var order = []

	$.each($('#subpage').find('a[class!="normallink"]'),function(){
		
		$(this).attr('href', $(this).attr('nohref'));
		$(this).removeAttr('nohref');
		
		var len = $(this).attr('href').length
		order.push( $(this).attr('href').substring(1,len-1) )
	});

	$.post("./ajax/changeOrder/", { 'order': order }, function (data) {
		if(! data.success ){
			alert(GENTICS.Aloha.Save.i18n("message.errorReorder"));
		}
	});
}

GENTICS.Aloha.Save.commitSave = function(){
	var content = "";

	var strTitle = '';
	var strContent = '';
	
	if( GENTICS.Aloha.Sections.CodeOriginal != undefined )
		GENTICS.Aloha.Sections.CodeOriginal();
	
	jQuery.each(GENTICS.Aloha.editables, function (index, editable) {
		switch(editable.getId()){
			case "title":
				strTitle = GENTICS.Aloha.Sections.stripHTML(editable.getContents()).trim();
			case "content":
				strContent = editable.getContents();
		}
	});
	
	strContent = strContent.replace( new RegExp('style *= *"[a-zA-Z0-9_ :;]*"','g'), "");
	
	var param = { title: strTitle , content: strContent }
	if ( GENTICS.Aloha.Save.newType != null ){
		param['type'] = GENTICS.Aloha.Save.newType;
	}
	
	jQuery.post(GENTICS.Aloha.Save.urlSave, param, 
		function(data){
			//console.log(data);
			if(data.success){
				if(data.redirect){
					window.location.href = data.redirect;
				}
			}else{
				alert(data.error);
				/*var error = jQuery('<p class="warning" >'+data.error+'</p>');
				$(".errorPanel").append(error);*/
			}
		}
	);
}
